//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#if _MSC_VER > 1000
#pragma once
#endif

#ifndef __CMUTEX_H_
#define __CMUTEX_H_

#include <elasys_server.h>
#include <interlocked.h>

class CMutex : public IMutex
{

public:
    CARAPI_(PInterface) Probe(
        /* [in]  */ REIID riid);

    CARAPI_(UInt32)AddRef();

    CARAPI_(UInt32)Release();

    CARAPI Aggregate(
        /* [in]  */ AggregateType type,
        /* [in]  */ PInterface pObject);

    CARAPI GetDomain(
        /* [out] */ PInterface *ppObject);

    CARAPI Lock(
        /* [out] */ WaitResult *pResult);

    CARAPI TryLock(
        /* [in]  */ Millisecond32 msTimeout,
        /* [out] */ WaitResult *pResult);

    CARAPI Unlock();

public: // static member function.
    STATIC ECode S_Create(
        /* [out] */ IMutex ** ppMutex);

public:
    CMutex(): m_hMutex(NULL) {}
    virtual ~CMutex();

    ECode Initialize();
    PVoid GetHandle() { return m_hMutex;}

private:
    PVoid m_hMutex;

private:
    Interlocked m_nRefs;
};

#endif //__CMUTEX_H_

